package com.huaboot.system.controller;

import com.huaboot.commont.core.BaseResponse;
import com.huaboot.commont.core.ResultUtils;
import com.huaboot.framework.security.utils.SecurityUtil;
import com.huaboot.system.service.AuthLoginService;
import com.huaboot.system.vo.AuthLoginReqVO;
import com.huaboot.system.vo.AuthLoginRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @Author ff
 * @Date 2023/8/27 1:21 下午
 * @Version 1.0
 */
@Tag(name = "管理后台 - 认证")
@RestController
@RequestMapping("/system/auth")
public class LoginController {


    @Autowired
    private AuthLoginService loginService;


    @PostMapping("/login")
    @PermitAll
    @Operation(summary = "使用账号密码登录")
    public BaseResponse<AuthLoginRespVO> login(@RequestBody @Valid AuthLoginReqVO reqVO) {
        return ResultUtils.success(loginService.loginByAccount(reqVO));
    }

    @PostMapping("token")
    @Operation(summary = "获取 accessToken")
    public BaseResponse<AuthLoginRespVO> token(String refreshToken) {
        AuthLoginRespVO token = loginService.getAccessToken(refreshToken);

        return ResultUtils.success(token);
    }
    @PostMapping("logout")
    @Operation(summary = "退出")
    public BaseResponse<?> logout(HttpServletRequest request) {
        loginService.logout(SecurityUtil.getAccessToken(request));

        return ResultUtils.success();
    }
}
